import os
import json
from PyQt5.QtWidgets import QMessageBox

CONFIG_PATH = os.path.expanduser("~/.csdn_downloader_config.json")

def save_cookie(cookie_data):
    """保存Cookie到用户目录"""
    try:
        with open(CONFIG_PATH, 'w') as f:
            json.dump({'cookies': cookie_data}, f)
    except Exception as e:
        QMessageBox.critical(None, "配置错误", f"保存配置失败：{str(e)}")

def load_cookie():
    """从用户目录加载Cookie"""
    try:
        if os.path.exists(CONFIG_PATH):
            with open(CONFIG_PATH, 'r') as f:
                config = json.load(f)
                return config.get('cookies', {})
        return {}
    except Exception as e:
        QMessageBox.warning(None, "配置警告", f"加载配置异常：{str(e)}")
        return {}